package com.wantup.springboot.boot.service.impl;

import com.wantup.springboot.boot.beans.TxPerson;
import com.wantup.springboot.boot.mapper.TxPersonMapper;
import com.wantup.springboot.boot.service.TxPersonService;
import com.wantup.springboot.boot.util.JsonData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
  * @author:  zhaoluowei
  * @date :   2018/11/7 12:09
  * @version  1.0
  */
@Service
@Transactional
public class TxPersonServiceImpl implements TxPersonService {

    @Resource
    private TxPersonMapper txPersonMapper;

    /**
     * 查询 不走 事务
     */
    @Override

    public List<TxPerson> getAllEmp() {
        return txPersonMapper.selectPersons();
    }

    @Override
    public JsonData addTxPerson(TxPerson txPerson) throws Exception {
        int addTxPerson = this.txPersonMapper.addTxPerson(txPerson);
        if (addTxPerson > 0) {
            return new JsonData(0,"添加成功");
        } else {
            return new JsonData(1,"添加失败");
        }

    }

    @Override
    public JsonData updateTxPerson(TxPerson txPerson) throws Exception {
        JsonData jsonData = null;
        boolean updateTxPerson = this.txPersonMapper.updateTxPerson(txPerson);
        if (updateTxPerson){
            jsonData = new JsonData(0,"更新成功");
        }else{
            jsonData = new JsonData(1,"更新失败");
        }
        return jsonData;
    }


    @Override
    public JsonData deleteTxPerson(Integer pid) throws Exception {
        JsonData jsonData = null;
        boolean deleteTxPerson = this.txPersonMapper.deleteTxPerson(pid);
        if (deleteTxPerson){
            jsonData = new JsonData(0,"删除成功");
        }else{
            jsonData = new JsonData(1,"删除失败");
        }
        return jsonData;
    }

    @Override
    public int getCountItem() throws Exception {

        return this.txPersonMapper.getCountTxPerson();
    }

    /**
     * web 端 获取总数
     * @return
     * @throws Exception
     */
    @Override
    public JsonData getCountTxPersonItem() throws Exception {
        JsonData jsonData = null;
        int countTxPerson = this.txPersonMapper.getCountTxPerson();
        if (countTxPerson >0){
            jsonData = new JsonData(0,"success",countTxPerson);
        }else{
            jsonData = new JsonData(1,"no data");
        }

        return jsonData;
    }


    @Override
    public TxPerson getTxPersonById(int pid) throws Exception {
        return this.txPersonMapper.getTxPersonById(pid);
    }
}
